const mysql = require('../db/mysql57');
const minio = require('../db/minio');

const fileDao = {
    // 获取用户头像的预签名URL
    async getAvatarUrl(uid) {
        let sql = `select
                browser,
                uri
            from User as u
            left join Avatar as a on u.avatarId = a.id
            where u.id = ?`;
        let data = [ uid ];
        let result = await mysql.sqlExec(sql, data);
        if (result.length === 0 || !result[0].browser) {
            return { success: true, url: '/static/default_avatar.jpg' };
        }
        let { browser, uri } = result[0];
        return minio.getDownloadUrl(browser, uri, 36000);
    },
    // 获取动态图片的预签名URL
    async getTreeHoleImgUrl(imgId) {
        if (!imgId) {
            return { success: true, url: '' };
        }
        let sql = `select
                browser, uri
            from PostImg
            where id = ?`;
        let data = [ imgId ];
        let result = await mysql.sqlExec(sql, data);
        if (result.length === 0 || !result[0].browser) {
            return { success: true, url: '' };
        }
        let { browser, uri } = result[0];
        return minio.getDownloadUrl(browser, uri, 36000);
    },
    // 获取活动图片的预签名URL
    async getActivityImgUrl(imgId) {
        if (!imgId) {
            return { success: true, url: '/static/activity_default.jpg' };
        }
        let sql = `select
                browser, uri
            from ActivityImg
            where id = ?`;
        let data = [ imgId ];
        let result = await mysql.sqlExec(sql, data);
        if (result.length === 0 || !result[0].browser) {
            return { success: true, url: '/static/activity_default.jpg' };
        }
        let { browser, uri } = result[0];
        return minio.getDownloadUrl(browser, uri, 36000);
    },
};

module.exports = fileDao;